package com.taxinfo.view.standard;

import com.taxinfo.component.progress.TableProgressIndicator;
import com.taxinfo.component.table.ConditionTable;
import com.taxinfo.pojo.dto.StandardSubDto;
import com.taxinfo.service.MainReportService;
import com.taxinfo.service.StandardSubService;
import com.taxinfo.utils.SpringUtil;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.List;

public class StandardSubTable extends ConditionTable<StandardSubDto> {

    private static final String MENU_CODE = "standard";

    private final StandardSubService mainReportService = (StandardSubService) SpringUtil.getBean("StandardSubServiceImpl");
    private final ObservableList<StandardSubDto> items = getItems();


    public StandardSubTable(List<StandardSubDto> items) {
        super(MENU_CODE, items, StandardSubDto.class);
        this.items.addAll(items);
        showAllInfos();
    }


    /**
     * 显示所有会员信息
     */
    public void showAllInfos() {
        clearData();
        setPlaceholder(new TableProgressIndicator());

        Task<List<StandardSubDto>> task = new Task<List<StandardSubDto>>() {
            @Override
            protected List<StandardSubDto> call() {
                List<StandardSubDto> beans = mainReportService.selectAllData("","");
                if (beans != null)
                    return beans;
                else
                    return new ArrayList<>(0);
            }
        };
        new Thread(task).start();
        task.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.size() != 0)
                items.addAll(newValue);
            else
                setPlaceholder(new Label("没有数据"));
        });
    }

    /**
     * 添加用户信息
     *
     * @param dto 用户信息
     */
    public void addBean(StandardSubDto dto) {
        items.add(dto);
    }
}